package com.niit.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CWork on 2019-5-23.
 */

public class spinnerAdapterActivity extends Activity{

    private Spinner spinner;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinneradapter);

        spinner = (Spinner)findViewById(R.id.spinnerAdapter);

        SimpleAdapter simpleAdapter = new SimpleAdapter(spinnerAdapterActivity.this, getData(),
                R.layout.items,new String[]{"ivLogo","appName"},new int[]{R.id.imageview,R.id.textview});

        spinner.setAdapter(simpleAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(spinnerAdapterActivity.this,
                        "您选择了第 "+(position+1)+"个",Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

    }

    public List<Map<String,Object>> getData(){
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("ivLogo",R.drawable.avatar9);
        map.put("appName","表情1");
        list.add(map);
        Map<String,Object> map1 = new HashMap<String,Object>();
        map1.put("ivLogo",R.drawable.avatar10);
        map1.put("appName","表情2");
        list.add(map1);
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("ivLogo",R.drawable.avatar8);
        map2.put("appName","表情3");
        list.add(map2);

        return list;
    }
}
